import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-08-25
 * Time: 21:32
 */
public class demo13 {
    public static void main(String[] args) throws InterruptedException {
        //把整个任务拆成10个小任务, 让10个线程去执行
        CountDownLatch countDownLatch = new CountDownLatch(10); //子任务的个数
        ExecutorService executorService = Executors.newFixedThreadPool(10); //10个线程的线程池
        for(int i = 0; i < 10; i++){
            int id = i;
            executorService.submit(()->{
                System.out.println("子任务开始执行" + id);
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    throw new RuntimeException(e);
                }
                System.out.println("子任务结束执行");
                countDownLatch.countDown(); //表示一个任务执行完
            });
        }
        countDownLatch.await(); //阻塞等待所以子任务执行完
        System.out.println("所有任务执行完毕");

    }
}
